package com.ctshk.rpc.goods.mapper.provider;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ctshk.rpc.goods.req.ShoppingSpecialPageReq;
import com.ctshk.rpc.order.goods.dto.ShoppingSpecialPageDTO;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.jdbc.SQL;

/**
 * 商城专题分页查询
 * @author 姜斌文
 * @since 2021-04-30
 */
public class ShoppingSpecialProvider {
    public String findShoppingSpecialPageByCondition(IPage<ShoppingSpecialPageDTO> page, ShoppingSpecialPageReq pageReq) {
        String sql = new SQL() {{
            SELECT("t.id,t.special_topics,t.special_picture_url,t.special_title,t.create_id,t.sort,t.is_display, t.gmt_create" +
                    ", (SELECT COUNT(1) FROM t_shopping_special_commodity ssc WHERE ssc.special_id = t.id) AS 'specialCommodityCount'");
            FROM("`ctshk-goods`.t_shopping_special t");
            WHERE("t.is_deleted = 0");
            if(pageReq.getUserIds() != null){
                WHERE("t.create_id in (${pageReq.userIds})");
            }
            if (StringUtils.isNotBlank(pageReq.getSpecialTitle())) {
                WHERE("t.special_title like concat('%',#{pageReq.specialTitle},'%')");
            }
            if (null != pageReq.getOperatorId()) {
                WHERE("t.operator_id = #{pageReq.operatorId}");
            }
            ORDER_BY("t.sort ASC, t.gmt_create DESC");
        }}.toString();
        return sql;
    }
}
